package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.ai.web.cache.Constants;
import com.ai.web.cache.UserContext;
import com.tiny.persistence.domain.WxKeywordConfig;


public class WxKeywordConfigRepoImpl extends BaseRepoImpl<WxKeywordConfig> {
	
	private static Logger logger = Logger.getLogger(WxKeywordConfigRepoImpl.class);
	
	@Override
	public List<WxKeywordConfig> getListByCondition(Map<String, Object> condition) {
		StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("from WxKeywordConfig as a where 1=1 ");
        
        //查询条件
        String keyword = null;
        if(condition.get("keyword")!=null){
        	keyword = String.valueOf(condition.get("keyword"));
        }
        //
        if(keyword!=null){
            sqlquery.append(" and a.keyword like :keyword"); 
        }
        Query query = em.createQuery(sqlquery.toString());
        if(keyword!=null){
            query.setParameter("keyword", "%"+keyword+"%");
        }
        
        UserContext uc = (UserContext) condition.get(Constants.USER_CONTEXT);
        query.setFirstResult((Integer) condition.get(Constants.KEY_RECORD_FROM));
        query.setMaxResults(uc.getPageSize());
        @SuppressWarnings("unchecked")
		List<WxKeywordConfig> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
	}
	
	@Override
	public int getCountByCondition(Map<String, Object> condition) {
		StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("select count(*)  from WxKeywordConfig as a where 1=1 ");
        
        //查询条件
        String keyword = null;
        if(condition.get("keyword")!=null){
        	keyword = String.valueOf(condition.get("keyword"));
        }
        //
        if(keyword!=null){
            sqlquery.append(" and a.keyword like :keyword"); 
        }
        Query query = em.createQuery(sqlquery.toString());
        if(keyword!=null){
            query.setParameter("keyword", "%"+keyword+"%");
        }
        
        UserContext uc = (UserContext) condition.get(Constants.USER_CONTEXT);
        query.setFirstResult((Integer) condition.get(Constants.KEY_RECORD_FROM));
        query.setMaxResults(uc.getPageSize());
        Object obj = query.getSingleResult();
		
		logger.info("======= count=" + obj);
		
		return Integer.parseInt(obj.toString());

	}
	
	
	


}
